<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\FangattrRequest;
use App\Models\Fangattr;
use Illuminate\Http\Request;

class FangattrController extends BaseController
{
    # 房源列表显示
    public function index(Fangattr $fangattr)
    {
        $data = Fangattr::get()->toArray();
        $data = treeLevel($data);
        return view("admin.fangattr.index",compact("data","fangattr"));
    }

    # 添加列表的显示
    public function create()
    {
        $data = Fangattr::where("pid",0)->get();
//        $data = treeLevel($data);
        return view("admin.fangattr.create",compact("data"));
    }

    # 添加数据处理
    public function store(FangattrRequest $request)
    {
//        dump($request->all());die;
        Fangattr::create($request->except(["_token","file"]));
        return redirect( route("admin.fangattr.index"))->with("success","添加成功");
    }

    # 修改房源属性的数据展示
    public function show(FangattrR $fangattr)
    {
        //
    }

    # 显示修改数据的列表
    public function edit(Fangattr $fangattr)
    {
        $data = Fangattr::where("pid",0)->get();
//        dump($data);die;
        return view("admin.fangattr.edit",compact("data","fangattr"));
    }

    # 修改数据的处理
    public function update(FangattrRequest $request, Fangattr $fangattr)
    {
        $data = $request->except(["_token","_method","file"]);
//        $data["field_name"] = !empty($data["field_name"]) ? $data["field_name"] : "";
        $fangattr->update($data);
//        dump($data);
        return redirect(route("admin.fangattr.index"))->with("success","修改成功");

    }


    public function destroy(Fangattr $fangattr)
    {
        $fangattr->delete();

        return ["status"=>200,"msg"=>"删除成功"];
    }
    # 删除封面图片
    public function deletePic(Request $request){
        $path = $request->get("path");
        $file_path = public_path($path);
        if(file_exists($file_path)){
            unlink($file_path);
        }
        Fangattr::where("id",$request->get("id"))->update(["icon" => ""]);
        return ["status" => 200,"msg" => "修改成功"];
    }
}
